package com.jiaai.web.xiaoke.family;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jiaai.auth.model.AccessUser;
import com.jiaai.exception.ServerException;
import com.jiaai.family.api.TFyroleApi;
import com.jiaai.family.model.TFyroleVO;
import com.jiaai.filter.aop.TokenIntercept;
import com.jiaai.util.RequestUtils;
import com.jiaai.util.RestResultGenerator;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * TODO
 * </p>
 *
 * @author 佳爱网络科技
 * @version V1.0
 * @title AppFyRoleController
 * @since 2017/12/27
 */
@RestController
@RequestMapping("/api/v1/app/fyRole")
public class AppFyRoleController {
    @Reference
    private TFyroleApi tFyroleApi;
    /**
     * 删除单个家族下的单个成员
     * @param id
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    @TokenIntercept(checkVisitor = true)
    public Object delete(Integer fyId,Integer id) throws ServerException {
        AccessUser accessUser = RequestUtils.getUser();
        try {
//            ((TFyroleVO)tFyroleApi.findById(fyId,accessUser.getUserId())).getFyRole()==1
            if(true){
                tFyroleApi.delete(fyId,id);
                return RestResultGenerator.genSuccessResult("删除成功");
            }else {
                throw new ServerException("暂无权限");
            }
        } catch (Exception e) {
            throw new ServerException("删除失败");
        }
    }
    /**
     * 添加家族成员
     * @param fyId,isNo
     * @return
     * @throws Exception
     */
    @PostMapping("/joinFamily")
    @TokenIntercept(checkVisitor = true)
    public Object joinFamily(Integer fyId,boolean isAccaus ) throws ServerException {
        AccessUser accessUser = RequestUtils.getUser();
        try {
            tFyroleApi.joinFamily(fyId,accessUser.getUserId(),isAccaus);
            return RestResultGenerator.genSuccessResult("添加成功");
        } catch (Exception e) {
            throw new ServerException("添加失败");
        }
    }

    /**
     * 修改家族成员
     * @param id,fyId,mId,fyRole
     * @return
     * @throws Exception
     */
    @PostMapping("/update")
    @TokenIntercept(checkVisitor = true)
    public Object update(Integer id,Integer fyId,Integer fyRole,String mId) throws ServerException {
        AccessUser accessUser = RequestUtils.getUser();
        try {
//            ((TFyroleVO)tFyroleApi.findById(fyId,accessUser.getUserId())).getFyRole()==1
            if(true){
                tFyroleApi.update(id,fyId,fyRole,mId);
                return RestResultGenerator.genSuccessResult("修改成功");
            }else {
                throw new ServerException("暂无权限");
            }
        } catch (Exception e) {
            throw new ServerException("修改失败");
        }
    }
    /**
     * 修改申请成员状态
     * @param id
     * @param applyState
     * @return
     * @throws Exception
     */
    @PostMapping("/changeState")
    @ResponseBody
    @TokenIntercept(checkVisitor = true)
    public Object changeState(Integer id,Integer applyState,Integer fyId,String mId) throws ServerException {
        AccessUser accessUser = RequestUtils.getUser();
        try {
//            ((TFyroleVO)tFyroleApi.findById(fyId,accessUser.getUserId())).getFyRole()
            if(true){
                tFyroleApi.changeState(id,applyState,fyId,mId);
                return RestResultGenerator.genSuccessResult("修改成功");
            }else  {
                throw new ServerException("暂无权限");
            }
        } catch (Exception e) {
            throw new ServerException("修改失敗");
        }
    }
    /**
     *查询单个成员信息
     * @param
     * @param fyId,mId
     * @return
     */
    @PostMapping("/findById")
    @ResponseBody
    public Object findById(Integer fyId,String mId) throws ServerException {
       return  tFyroleApi.findById(fyId,mId);
    }

    /**
     * 获取单个家族的所有成员
     * @param curr
     * @param tFyroleVO
     * @return
     * @throws Exception
     */
    @GetMapping("/allrole")
    @ResponseBody
    public Object allrole(@RequestParam(defaultValue = "0", required = false) Integer curr, TFyroleVO tFyroleVO )
            throws Exception {
        return RestResultGenerator.genSuccessResult(tFyroleApi.allrole(curr, tFyroleVO));
    }

}
